import { useRoute } from "vue-router";
import { idHelper } from "@renderer/store/pinia/helper";
import { useTabManagerStore } from "@renderer/store/pinia/tabManager";
import { useViewManagerStore } from "@renderer/store/pinia/viewManager";
import { useUserOpStore } from "@renderer/store/pinia/userOp";
import { computed } from "vue";
import { EnumViewMode } from "@renderer/assets/types";

export const useGetCurrentView = () => {
  const route = useRoute();
  const splitArr =  route.fullPath.split("/");
  const tabId = splitArr[2];
  const {
    tabType, patientUID, seriesId, viewMode,viewType
  } = idHelper.resolveTabId(tabId)

  const viewId = idHelper.createViewId({
    viewMode, viewType,seriesId
  })

  return {
    tabType,
    patientUID,
    seriesId,
    viewMode,
    viewType,
    viewId,
    tabId
  };
}

export const getActiveTabViewOp = () => {
   const tabStore = useTabManagerStore()
   const viewStore = useViewManagerStore()
   const opStore = useUserOpStore()

  const activeViewRef = computed<any>(() => {
    return viewStore.tabActiveViewIdObj[tabStore.activeTabId]
  })

  const activeOpObjRef = computed(() => {
    if (!activeViewRef.value?.viewMode) {
      return  null
    }
     if (activeViewRef.value.viewMode === EnumViewMode.normal) {
       return opStore.viewOpObj[activeViewRef.value.groupId]
     }
     return opStore.viewOpObj[activeViewRef.value.viewId]
  })

  return {
     activeTabId: tabStore.activeTabId,
     activeViewRef,
     activeOpObjRef
  }
}
